package com.jyb.qiyefuwu.view.fragment.usercenter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jyb.qiyefuwu.BR;
import com.jyb.qiyefuwu.R;
import com.jyb.qiyefuwu.base.MBaseFragment;
import com.jyb.qiyefuwu.databinding.FragmentFollowServiceBinding;
import com.jyb.qiyefuwu.entity.FollowServiceEntity;
import com.jyb.qiyefuwu.view.adpater.FollowServiceAdpater;
import com.jyb.qiyefuwu.viewmodel.ServiceFollowListViewModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.jetbrains.annotations.NotNull;

public class ServiceFollowListFragment extends AppLazyFragment {


    private FragmentFollowServiceBinding mBinding;
    private ServiceFollowListViewModel mViewModel;
    private FollowServiceAdpater mAdpater;


    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_follow_service, container, false);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        super.initView();
        mViewModel = new ViewModelProvider(this).get(ServiceFollowListViewModel.class);
        mBinding.setVariable(BR.viewmodel,mViewModel);
        mBinding.setLifecycleOwner(this);
        initRecyclerView();
        initObserver();
    }

    private void initRecyclerView(){
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdpater = new FollowServiceAdpater(getActivity());
        mBinding.recyclerView.setAdapter(mAdpater);
        mAdpater.setOnItemClick(new AdapterOnItemClick<FollowServiceEntity>() {
            @Override
            public void onItemClick(FollowServiceEntity followServiceEntity, int position) {
                if(position==-1){
                    mViewModel.updateFollowType(followServiceEntity);
                }else{

                }
            }
        });

        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mViewModel.getList(false);
            }

            @Override
            public void onLoadMore() {
                mViewModel.getList(true);
            }
        });
    }


    private void initObserver(){
        mViewModel.listdata.observe(this,listdata ->{
            if(listdata.size()<10){
                mBinding.recyclerView.setNoMore(true);
            }
            if (mViewModel.loadmore) {
                mBinding.recyclerView.loadMoreComplete();
                mAdpater.addDatas(listdata);

            } else {
                mBinding.recyclerView.refreshComplete();
                if (listdata.size() == 0) {
                    mBinding.recyclerView.setVisibility(View.GONE);
                    mBinding.errorView.setVisibility(View.VISIBLE);
                } else {
                    mBinding.errorView.setVisibility(View.GONE);
                    mBinding.recyclerView.setVisibility(View.VISIBLE);
                    mAdpater.setDatas(listdata);
                }
            }
        });

        mViewModel.mToast.observe(getViewLifecycleOwner(),message -> MToast.showLongToast(getActivity(),message));
        mViewModel.mLoading.observe(getViewLifecycleOwner(),type->{
            switch (type){
                case BaseViewModel.SHOW_LODING:
                    DialogHelper.showProgressDialog(getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                    break;
                case BaseViewModel.DISMISS_LODING:
                    DialogHelper.dismissProgressDialog();
                    break;
                case BaseViewModel.FINISH_AND_DISMISS_LODING:
                    DialogHelper.dismissProgressDialog();
                    getActivity().finish();
                    break;
            }
        });
    }


    public static ServiceFollowListFragment newInstance(){
        return new ServiceFollowListFragment();
    }

    @Override
    protected void onLazyLoad() {
        mViewModel.getList(false);
    }
}
